﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyBetImporter.DAL;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace MyBetImporter
{
    [DataContractAttribute(IsReference = true)]
    [Serializable()]
    public class RankObject
    {

        private string _friendlyName;
        private int _customContestId;
        private long _totalScore;
        private int _goodPredictionCount;
        private int _goodScoreCount;
        private int _eventCount;
        private int _matchCount;
        private int _rank;
        private Guid _userId;
        private string _userName;

        [Key()]
        [DataMemberAttribute()]
        public Guid UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        [DataMemberAttribute()]
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        [DataMemberAttribute()]
        public string FriendlyName
        {
            get { return _friendlyName; }
            set { _friendlyName = value; }
        }

        [DataMemberAttribute()]
        public int Rank
        {
            get { return _rank; }
            set { _rank = value; }
        }

        [DataMemberAttribute()]
        public int CustomContestId
        {
            get { return _customContestId; }
            set { _customContestId = value; }
        }

        [DataMemberAttribute()]
        public long TotalScore
        {
            get { return _totalScore; }
            set { _totalScore = value; }
        }

        [DataMemberAttribute()]
        public int GoodPredictionCount
        {
            get { return _goodPredictionCount; }
            set { _goodPredictionCount = value; }
        }

        [DataMemberAttribute()]
        public int GoodScoreCount
        {
            get { return _goodScoreCount; }
            set { _goodScoreCount = value; }
        }

        [DataMemberAttribute()]
        public int EventCount
        {
            get { return _eventCount; }
            set { _eventCount = value; }
        }

        [DataMemberAttribute()]
        public int MatchCount
        {
            get { return _matchCount; }
            set { _matchCount = value; }
        }

        ///// <summary>
        ///// Create object from database information
        ///// </summary>
        ///// <param name="m"></param>
        ///// <returns></returns>
        //public static RankObject CopyFrom(vw_rank rnk)
        //{
        //    RankObject rankTgt = new RankObject();
        //    rankTgt.CustomContestId = rnk.CustomContestId;
        //    rankTgt.TotalScore = rnk.TotalScore;
        //    rankTgt.GoodPredictionCount = rnk.GoodPredictionCount;
        //    rankTgt.GoodScoreCount = rnk.GoodScoreCount;
        //    rankTgt.EventCount = rnk.EventCount;
        //    rankTgt.MatchCount = rnk.MatchCount;
        //    rankTgt.UserId = rnk.UserId;
        //    rankTgt.UserName = rnk.UserName;
        //    return rankTgt;
        //}

        /// <summary>
        /// Get user ranks of a given contest
        /// </summary>
        /// <param name="dbContext">Database connection</param>
        /// <param name="contestId">Contest identifier</param>
        /// <returns></returns>
        public static List<RankObject> GetRanksInDatabase(ParieurProDBEntities dbContext, int contestId)
        {
            IEnumerable<RankObject> ranksResult = dbContext.ExecuteStoreQuery<RankObject>
                (String.Format(@"select Ranks.*, s.SubscriptionId, au.UserName, au.UserId from Ranks rnk
                   left join Subscriptions s on s.SubscriptionId=rnk.SubscriptionId
                   left join aspnet_Users au on au.UserId = s.UserId
                   where s.CustomContestId={0} ORDER BY rnk.TotalScore DESC", contestId));

            List<RankObject> contestRanks = ranksResult.ToList();
            return contestRanks;
        }

        public static bool AddOrUpdate(ParieurProDBEntities dbContext, Ranks rank)
        {
            ParieurProDBEntities db = dbContext;
            try
            {
                var query = (from rnk in db.Ranks
                             where rnk.SubscriptionId == rank.SubscriptionId
                             select rnk);
                IList<Ranks> existingRank = query.ToList();

                if (existingRank.Count < 1)
                {
                    // Add
                    db.Ranks.AddObject(rank);
                    dbContext.SaveChanges();
                    return true;
                }
                else
                {
                    // Update
                    existingRank[0].EventCount = rank.EventCount;
                    existingRank[0].GoodPredictionCount = rank.GoodPredictionCount;
                    existingRank[0].GoodScoreCount = rank.GoodScoreCount;
                    existingRank[0].MatchCount = rank.MatchCount;
                    existingRank[0].SubscriptionId = rank.SubscriptionId;
                    existingRank[0].TotalScore = rank.TotalScore;
                    existingRank[0].CurrentRank = rank.CurrentRank;
                    existingRank[0].RankDifferential = rank.RankDifferential;
                    //dbContext.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return false;
            }
        }

    }
}